<?php
if(!defined('IN_INDEX'))
{
	posterror("ACCESS DENIED", "This page is hidden.");
	exit;
}
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}

if (isset($_POST['delete_pm']))
{
  $id = array();
  $id = $_POST['removeid'];
  //print_r($id);
  if (count($id) > 0)
  {
     foreach ($id as $removeid)
     {
        deletePmById2($removeid);
     }
  }
	
	echo"<br /><br /><br /><br /><br /><br /><br /><br /><br />
		<center><meta http-equiv=\"refresh\"content=\"2;url=index.php?action=profile&amp;sub=pm_sent\">";
		echo "<h2>" . ucfirst(translate('pmdeleted')) . "</h2>\n";
		echo "<br/>" . translate('redirectedin'). " <a href=\"index.php?action=profile&amp;sub=pm_sent\">" . translate('clickhere') . "</a>.</center>
		<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
		exit;
}

$PM_OUTBOX = listPM_outbox($USER->getId(), "ORDER BY -pm_date");
$dateformat = $CONFIG['date_format'];
$dateformat_long = $CONFIG['date_format_long'];
$page_url = "index.php?action=profile&amp;sub=pm_sent";
$pm_per_page = 5;
$start = 0;

		if(checkSession())
		{
			$pm_per_page = $USER->getPmPerPage();
		}
		if(isset($_REQUEST['p']))
		{
			$start = ($_REQUEST['p'] * $pm_per_page) - $pm_per_page;
		}
		
		$end = $start + $pm_per_page;
		if($end > count($PM_OUTBOX))
		{
			$end = count($PM_OUTBOX);
		}
?>

<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
        &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
		&gt; <a href="index.php?action=profile&amp;id=<?php echo $USER->getId(); ?>"><?php echo ucf($USER->getFirstname()) . " " . ucf($USER->getLastname()); ?></a>
        &gt; <a href="index.php?action=profile&amp;sub=pm" title='<?php echo translate('pm'); ?>'><?php echo translate('pm'); ?></a>
        &gt; <a href="index.php?action=profile&amp;sub=pm_sent" title='<?php echo translate('outbox'); ?>'><?php echo translate('outbox'); ?></a>  
</div>

   <br />
        <h1>
        <a style='color:#555555;' href="index.php?action=profile&amp;sub=pm"><img src="img/pm_unread.png" height="20"  /> <?php echo translate('inbox'); ?></a>
        <a style='color:#555555;' href="index.php?action=profile&amp;sub=pm_new"><img src="img/pm_new.png" height="20"  /> <?php echo translate('newprivatemessage'); ?></a>
        </h1>
        <br />
        <h1><?php echo translate('outbox'); ?></h1>
        <br />
         <form method="post" onsubmit="if(!confirm('<?php echo translate('deletewarning2'); ?>'))return false;" action="index.php?action=profile&amp;sub=pm_sent" />
        <table border="0" width="100%" cellpadding="2" cellspacing="0" style="border-top:solid thin; border-left:solid thin; border-right:solid thin;">
            <?php
			$counter = 0;
			$showing = 0;
			
			if(count($PM_OUTBOX) > 0)
			{
				$color1 = "#ffffff"; 
				$color2 = "#F2F2F2";
				
				foreach($PM_OUTBOX as $k => $v)
				{
					if($counter >= $start && $counter < $end)
					{
						
					switch($v['is_read'])
					{
						case 1:
							$pm_img = "<img src=\"img/pm_read.png\" title='" . translate('pm_read') . "' alt=\"" . translate('pm_read') . "\" />";
							break;
						case 2:
							$pm_img = "<img src=\"img/pm_replied.png\" title='" . translate('pm_replied') . "' alt=\"" . translate('pm_replied') . "\" />";
							break;
						default:
							$pm_img = "<img src=\"img/pm_unread.png\" title='" . translate('pm_unread') . "' alt=\"" . translate('pm_unread') . "\" />";
							break;
					}
					
					if (!isset($currentcolor))
					 { 
					 $currentcolor = $color2; 
					 } 
					 if ($currentcolor == $color1) 
					 { 
					 $currentcolor = $color2; 
					 } 
					 else 
					 { 
					 $currentcolor = $color1; 
					 } 
					
					echo "
					<tr bgcolor='". $currentcolor ."'>
						<td style='border-bottom: solid thin;' rowspan='2' align='left'>";
						
						if(selectAvatarPath($v['to_id']) != "")
						{
							$profile_pic_path = $CONFIG['user_data_path'] . $v['to_id'] . "/thumbs_" . selectAvatarPath($v['to_id']);
							if(file_exists($profile_pic_path))
							{
								echo "<a href='index.php?action=profile&amp;id=" . $v['from_id'] . "'><img style='background: url(" . $profile_pic_path . ") no-repeat 50%; height: 60px; width: 60px;' src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a>\n";
							}
							else
							{
								echo $profile_pic_path;
							}
						}
						else
						{
							if($USER->getGender() == "female")
							{
								echo "<center><a href='index.php?action=profile&amp;id=" . $v['to_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" class=\"profile_picture\" /></a></center>\n";
							}
							else
							{
								echo "<center><a href='index.php?action=profile&amp;id=" . $v['to_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" class=\"profile_picture\" /></a></center>\n";
							}
						}
						
				  echo "</td>
				<td align='left' style='border-bottom: solid thin;' rowspan='2'>
					" . ucf(translate('to')) ." <a style='color:#555555;' href=\"index.php?action=profile&amp;id=" . $v['to_id'] . "\"><b>" . $v['to'] . "</b></a>
					<br>" . formatDate($v['date'], $dateformat_long) . "
				</td>
				
				<td align='left' style='border-bottom: solid thin;' rowspan='2'>
					<a style='color:#555555;' href=\"index.php?action=profile&amp;sub=pm_show&amp;pid=" . $v['id'] . "\"><b>" . ($v['subject']) . "</b></a>
					<br>" . strip_tags(substr($v['body'], 0, 95)) . "..
				</td>
				
				<td style='border-bottom: solid thin;' rowspan='2'>
					" . $pm_img . " <input type=\"checkbox\" name=\"removeid[]\" value=\"" . $v['id'] . "\" />
					</td>
				</tr>
					
					<tr bgcolor='". $currentcolor ."'></tr>\n";
						
					$showing++;
					}
					$counter++;
				} 
				
			}
			else
			{
				echo "
				<tr>
					<td colspan=\"5\" style='border-bottom: solid thin;'><b>" . ucf(translate('no_pm_recieved')) . "</b></td>
				</td>\n";
			}
			?>      
        </table>
        <br />
        <div style="padding-left:860px;"><input style="font-weight:bold; color:#F00;" name="delete_pm" type="submit" value=" <?php echo ucf(translate('delete')); ?> "  />
        </form></div>
        
    <!-- Display pagenumber START -->
    <center>
    	<?php
    	$post_text = $LANG['showing_pm'];
    	$post_text = str_replace('VAR1', $showing, $post_text);
    	$post_text = str_replace('VAR2', count($PM_OUTBOX), $post_text);
      if(count($PM_OUTBOX) >  $USER->getPmPerPage())
      {
		echo $post_text . "<br />";
        displayPaging(count($PM_OUTBOX), $USER->getPmPerPage(), $page_url, 'p', '<b>[', ']</b>'); 
     	}
      ?>
    </center>
    <!-- Display pagenumber END -->